package com.knife.changerecord.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.knife.changerecord.app.service.ChangeRecordTxHeaderService;
import com.knife.changerecord.domain.ChangeRecordTxHeader;
import com.knife.changerecord.domain.ChangeRecordTxLine;
import com.knife.changerecord.infra.mapper.ChangeRecordTxHeaderMapper;
import com.knife.changerecord.infra.mapper.ChangeRecordTxLineMapper;
import com.knife.lov.infra.annotations.KnifeProcessLovValue;
import com.knife.oauth.domain.entity.IamUser;
import com.knife.oauth.infra.cache.v2.KnifeIamUserRedisCache;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 *
 */
@Service
public class ChangeRecordTxHeaderServiceImpl extends ServiceImpl<ChangeRecordTxHeaderMapper, ChangeRecordTxHeader>
        implements ChangeRecordTxHeaderService {
    @Resource
    private ChangeRecordTxLineMapper changeRecordTxLineMapper;
    @Resource
    private KnifeIamUserRedisCache knifeIamUserRedisCache;

    @Override
    @KnifeProcessLovValue
    public List<ChangeRecordTxHeader> selectChangeRecord(Long tenantId, String moduleType, Long moduleId) {
        // 获取记录关联的事务头
        LambdaQueryWrapper<ChangeRecordTxHeader> changeRecordTxHeaderLambdaQueryWrapper = new LambdaQueryWrapper<>();
        changeRecordTxHeaderLambdaQueryWrapper
                .eq(ChangeRecordTxHeader::getModuleId, moduleId)
                .eq(ChangeRecordTxHeader::getModuleType, moduleType)
                .eq(ChangeRecordTxHeader::getTenantId, tenantId)
                .orderByDesc(ChangeRecordTxHeader::getId)
        ;
        List<ChangeRecordTxHeader> changeRecordTxHeaders = baseMapper.selectList(changeRecordTxHeaderLambdaQueryWrapper);
        List<Long> headerIdList = changeRecordTxHeaders.stream().map(ChangeRecordTxHeader::getId).collect(Collectors.toList());
        // 针对事务头获取出字段
        List<ChangeRecordTxLine> changeRecordTxLines = new ArrayList<>();
        LambdaQueryWrapper<ChangeRecordTxLine> changeRecordTxLineLambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (CollectionUtils.isNotEmpty(headerIdList)){
            changeRecordTxLineLambdaQueryWrapper.in(ChangeRecordTxLine::getTxHeaderId, headerIdList);
            changeRecordTxLines = changeRecordTxLineMapper.selectList(changeRecordTxLineLambdaQueryWrapper);
        }
        Map<Long, List<ChangeRecordTxLine>> lineMap = changeRecordTxLines.stream().collect(Collectors.groupingBy(ChangeRecordTxLine::getTxHeaderId));
        for (ChangeRecordTxHeader changeRecordTxHeader : changeRecordTxHeaders) {
            IamUser iamUserParam = new IamUser();
            iamUserParam.setId(changeRecordTxHeader.getOperatorId());
            IamUser iamUser = knifeIamUserRedisCache.getCache(iamUserParam);
            if (Objects.nonNull(iamUser)){
                if (StringUtils.isNotEmpty(iamUser.getRealName())){
                    changeRecordTxHeader.setCreatedByName(iamUser.getLoginName()+"("+iamUser.getRealName()+")");
                }else {
                    changeRecordTxHeader.setCreatedByName(iamUser.getLoginName());
                }
            }
            List<ChangeRecordTxLine> lineList = lineMap.get(changeRecordTxHeader.getId());
            if (CollectionUtils.isNotEmpty(lineList)){
                lineList.sort(Comparator.comparing(ChangeRecordTxLine::getFieldName));
                changeRecordTxHeader.setChangeRecordTxLineList(lineList);
            }else {
                changeRecordTxHeader.setChangeRecordTxLineList(Collections.emptyList());
            }
        }
        return changeRecordTxHeaders;
    }
}




